package com.example.reactandroid.wifi;

import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.example.reactandroid.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/12.
 */

public class WifiActivity extends AppCompatActivity implements View.OnClickListener{

    private Button mWifiOpenButton;
    private Button mWifiCloseButton;
    private Button mWifiDataListButton;
    private ListView mWifiListView;

    List<String> listdata = new ArrayList<>();

    private WifiHotUtil wifiHotUtil;
    private WifiAdmin wifiAdmin;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);

        mWifiOpenButton = (Button) findViewById(R.id.wifi_open);
        mWifiCloseButton = (Button) findViewById(R.id.wifi_close);
        mWifiDataListButton = (Button) findViewById(R.id.wifi_datalist);
        mWifiListView = (ListView) findViewById(R.id.wifi_listview);

        mWifiOpenButton.setOnClickListener(this);
        mWifiCloseButton.setOnClickListener(this);
        mWifiDataListButton.setOnClickListener(this);

        //实例化WiFi热点工具类
        wifiHotUtil = new WifiHotUtil(this);
        wifiAdmin = new WifiAdmin(this);

        List<ScanResult> listScanResult = wifiHotUtil.getWifiList();
        for(ScanResult item : listScanResult){
            listdata.add(item.SSID);
        }
        mWifiListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listdata));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.wifi_open:
                //创建热点
                wifiHotUtil.startWifiAp("defaultHotName", "");
                break;
            case R.id.wifi_close:
                //关闭热点
                wifiHotUtil.closeWifiAp();
                break;
            case R.id.wifi_datalist:
                String temp = wifiAdmin.getWifiInfo();
                Toast.makeText(getApplicationContext(), temp,Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
}
